package JDBC;

import util.JDBCUtils;

import java.sql.*;
import java.util.Scanner;

public class jdbc_logen {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    public static void main(String[] args) {
        //键盘录入,接受用户名和密码
        Scanner sc = new Scanner(System.in);
        String username = sc.next();
        String password = sc.next();
        boolean flag =  new jdbc_logen().login(username,password);
        if(flag)
        {
            System.out.println("成功");
        }
        else
        {
            System.out.println("失败");
        }
    }
    public boolean login(String username,String password)
    {
        if(username==null||password==null)
        {
            return false;
        }
        //连接数据库
        try {
            conn = DriverManager.getConnection("jdbc:mysql:///db","root","root");
            String sql = "select * from USER where username='"+username+"' and password='"+password+"'";
            Statement stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            return rs.next();
            //有下一行返回true
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,stmt,conn);
        }
        return false;
    }
}
